<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="utf-8">
		<title>在线用户</title>
		<link href="../../static/component/pear/css/pear.css" rel="stylesheet" th:href="@{/static/component/pear/css/pear.css}"/>
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">用户名</label>
							<div class="layui-input-inline">
								<input type="text" name="username" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">登录地址</label>
							<div class="layui-input-inline">
								<input type="text" name="ipaddr" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="useronline-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="useronline-table" lay-filter="useronline-table"></table>
			</div>
		</div>

		<script type="text/html" id="useronline-toolbar">
		    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        强退
		    </button>
		</script>

		<script type="text/html" id="useronline-bar">
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">强退</button>
		</script>

		<script type="text/html" id="useronline-status">
			{{#if (d.status == 'on_line') { }}
			<button class="layui-btn layui-btn-xs layui-btn-radius layui-btn-green">在线</button>
			{{# }else if(d.status == 'off_line'){ }}
			<button class="layui-btn layui-btn-xs layui-btn-radius layui-btn-danger">离线</button>
			{{# } }}
		</script>


		<script type="text/html" id="useronline-startTimestamp">
			{{layui.util.toDateString(d.startTimestamp, 'yyyy-MM-dd HH:mm:ss')}}
		</script>
		<script type="text/html" id="useronline-lastAccessTime">
			{{layui.util.toDateString(d.lastAccessTime, 'yyyy-MM-dd HH:mm:ss')}}
		</script>

		<script src="../../static/component/layui/layui.js" th:src="@{/static/component/layui/layui.js}"></script>
		<script src="../../static/component/pear/pear.js" th:src="@{/static/component/pear/pear.js}"></script>
		<script>
			layui.use(['table', 'form', 'jquery','common', 'dropdown'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;
				let dropdown = layui.dropdown;

				let MODULE_PATH = "operate/";

				let cols = [
					[{
							type: 'checkbox'
						},
						{
							title: '会话编号',
							field: 'sessionId',
							align: 'center'
						},
						{
							title: '登录名称',
							field: 'loginName',
							align: 'center'
						},
						{
							title: '主机',
							field: 'ipaddr',
							align: 'center'
						},
						{
							title: '登录地点',
							field: 'loginLocation',
							align: 'center'
						},
						{
							title: '浏览器',
							field: 'browser',
							align: 'center'
						},
						{
							title: '操作系统',
							field: 'os',
							align: 'center'
						},
						{
							title: '会话状态',
							field: 'status',
							align: 'center',
							templet: '#useronline-status'
						},
						{
							title: '登录时间',
							field: 'startTimestamp',
							align: 'center',
							templet: '#useronline-startTimestamp',
							width: 170
						},
						{
							title: '最后访问时间',
							field: 'lastAccessTime',
							align: 'center',
							templet: '#useronline-createTime',
							width: 170
						},
						{
							title: '操作',
							toolbar: '#useronline-bar',
							align: 'center'
						}
					]
				]

				table.render({
					elem: '#useronline-table',
					url: '/system/useronline/useronlinequery',
					method:'post',
					page: true,
					limits: [5,10,15,20],
					limit: 5, //每页默认显示的数量
					cols: cols,
					skin: 'line',
					toolbar: '#useronline-toolbar',
					parseData:function(res){
						return {
							"code" : res.code, //解析接口状态
							"msg" : res.msg, //解析提示文本
							"data" : res.data.records, //解析数据列表
							"count" : res.data.total
						}
					}
				});

				table.on('tool(useronline-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					}
				});

				table.on('toolbar(useronline-table)', function(obj) {
					if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(useronline-query)', function(data) {
					table.reload('useronline-table', {
						where: data.field
					})
					return false;
				});

				window.remove = function(obj) {
					layer.confirm('确定要注销该在线用户', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: "/system/useronline/remove",
							dataType: 'json',
							type: 'post',
							data:JSON.stringify({"sessionId":obj.data['sessionId']}),
							contentType:'application/json',
							success: function(result) {
								layer.close(loading);
								if (result.code ===0) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										table.reload('useronline-table');
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.batchRemove = function(obj) {
					var checkIds = common.checkField(obj,'sessionId');
					if (checkIds === "") {
						layer.msg("未选中数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}
					layer.confirm('确定要注销这些在线用户', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: "/system/useronline/remove",
							dataType: 'json',
							type: 'post',
							data:JSON.stringify({"sessionId":checkIds}),
							contentType:'application/json',
							success: function(result) {
								layer.close(loading);
								if (result.code ===0) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										table.reload('useronline-table');
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}
			})
		</script>
	</body>
</html>
